package com.lh.web.controller.ovmalUser;

import com.lh.common.entity.admin.User;
import com.lh.common.enums.IsDel;
import com.lh.common.enums.TrueFalseEnum;
import com.lh.common.enums.UserTypeEnum;
import com.lh.common.utils.*;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.ovmalUser.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FH
 * @ClassName: UserAction
 * @Description: (用户模块)
 * @date: 2020-05-12 9:33
 */
@Api(value="UserAction",tags={"用户管理"})
@RestController
@RequestMapping("ovmalUser")
public class UserAction extends AbstractController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     */
    @ApiOperation(value = "查询所有用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "开始时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "结束时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "type", value = "用户类型", required = false, dataTypeClass= UserTypeEnum.class)
    })
    @GetMapping("/getUserList")
    @RequiresPermissions("ovmalUser:getUserList")
    public Result<User> getUserList(String name, String phone, UserTypeEnum type,String startTime,String endTime, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("name", name);
        params.put("phone", phone);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        if(type != null) {
            params.put("type", type.getValue());
        }
        PageUtils<User> userMyPage = userService.queryUserList(params,pageNum,pageSize);
        return Result.ok(userMyPage);
    }

    /**
     * 新增机器人
     */
    @ApiOperation(value = "新增机器人")
    @PostMapping("/addRobot")
    @RequiresPermissions("ovmalUser:addRobot")
    public Result<User> addRobot(@RequestBody User user) {
        try {
            int i = userService.queryUserRobot(user.getPhone());
            if(i>0){
                return Result.error("机器人手机号已存在!");
            }
            user.setPassword(MD5Encrypt.md5Small("123456"));
            user.setType(UserTypeEnum.ROBOT);
            user.setStatus(TrueFalseEnum.TRUE);
            user.setIsDel(IsDel.NO);
            userService.save(user);
            return Result.ok(user);
        } catch (Exception e) {
            return Result.error("新增机器人异常!");
        }
    }

    /**
     * 导入用户
     */
    @ApiOperation(value = "导入用户")
    @PostMapping("/importUser")
    @RequiresPermissions("ovmalUser:importUser")
    public Result importUser(@RequestParam("file") MultipartFile file) throws Exception {
        List<User> list = ExcelUtils.readExcel( User.class, file);
        if (CollectionUtils.isEmpty(list)){
            AssertUtil.assertNotNull(list, Constant.RESULT.FI1000, "导入数据不能为空！");
        }else {
            userService.batchInsertUser(list);
        }
        return Result.ok();
    }

    /**
     * 导出用户
     */
    @ApiOperation(value = "导出用户")
    @GetMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "开始时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "结束时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "type", value = "用户类型", required = true, dataTypeClass= UserTypeEnum.class)
    })
    @RequiresPermissions("ovmalUser:export")
    public void export(String name, String phone, UserTypeEnum type,String startTime,String endTime, HttpServletResponse response) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        AssertUtil.assertNotNull(type, Constant.RESULT.FI1000, "未选择用户类型!");
        if(type != null) {
            params.put("type", type.getValue());
        }
        params.put("name", name);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("phone", phone);
        PageUtils pageUtils = userService.queryUserList(params, 0, Integer.MAX_VALUE);
        ExcelUtilJXL.exportExcelToBrowserForJSP(User.class, pageUtils.getList(),"用户信息.xls",response);
    }
}
